package top.zhdev.community.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.zhdev.community.entity.Comment;
import top.zhdev.community.entity.DiscussPost;
import top.zhdev.community.entity.Event;
import top.zhdev.community.event.EventProducer;
import top.zhdev.community.service.CommentService;
import top.zhdev.community.service.DiscussPostService;
import top.zhdev.community.utils.CommunityConstant;
import top.zhdev.community.utils.HostHolder;
import top.zhdev.community.utils.RedisKeyUtil;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 评论处理器
 */
@Controller
@RequestMapping("/comment")
public class CommentController implements CommunityConstant {

    private HostHolder hostHolder;
    private CommentService commentService;
    private DiscussPostService discussPostService;
    private EventProducer eventProducer;

    @Resource
    private RedisTemplate redisTemplate;

    @Autowired
    public void setHostHolder(HostHolder hostHolder){
        this.hostHolder = hostHolder;
    }

    @Autowired
    public void setCommentService(CommentService commentService){
        this.commentService = commentService;
    }

    @Autowired
    public void setDiscussPostService(DiscussPostService discussPostService){
        this.discussPostService = discussPostService;
    }

    @Autowired
    public void setEventProducer(EventProducer eventProducer){
        this.eventProducer = eventProducer;
    }

    /**
     * 添加 回帖信息
     * @param discussPostId
     * @param comment
     * @return
     */
    @PostMapping("/add/{discussPostId}")
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment){
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0); // 0代表什么意思
        comment.setCreateTime(new Date());
        commentService.addComment(comment);

        // 触发评论事件,有人评论了帖子，系统发送站内信，通知用户被评论
        Event event = new Event()
                .setTopic(TOPIC_COMMENT)
                .setUserId(hostHolder.getUser().getId())
                .setEntityType(comment.getEntityType())
                .setEntityUserId(comment.getEntityId())
                .setData("postId",discussPostId);

        if(comment.getEntityType() == ENTITY_TYPE_POST){
            DiscussPost target = discussPostService.getDiscussPostById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        } else if (comment.getEntityType() == ENTITY_TYPE_COMMENT) {
            Comment target = commentService.getCommentsByEntityId(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }
        eventProducer.sendEvent(event);
        // 触发发帖事件，评论帖子时，帖子的评论数量更改了，需要更新elasticSearch中的数据
        if (comment.getEntityType() == ENTITY_TYPE_POST){
            event = new Event()
                    .setTopic(TOPIC_PUBLISH)
                    .setUserId(hostHolder.getUser().getId())
                    .setEntityType(ENTITY_TYPE_POST)
                    .setEntityId(discussPostId);

            // 事件处理
            eventProducer.sendEvent(event);
            // 计算帖子分数
            String redisKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(redisKey,discussPostId);
        }
        return "redirect:/discuss/detail/" + discussPostId;
    }
}
